package wn.tcpip.mq.consumer.config.stateQuery;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.MessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.jms.support.converter.SimpleMessageConverter;

import javax.jms.Queue;

@Configuration
@Description(value = "state")
public class GpsStateMQConfig {
	public static final String QUEUE_GPS = "gpsStateInfo";
	@Bean(name="stateInfoQueue")
	public Queue stateInfoQueue() {
		return new ActiveMQQueue(QUEUE_GPS);
	}


	@Bean(name="textMessageListenerAdapter2")
	public MessageListenerAdapter messageListenerAdapter2() {
		MessageListenerAdapter adapter = new MessageListenerAdapter();
		adapter.setMessageConverter(new SimpleMessageConverter());
		adapter.setDelegate(textConsumerListener2());
		return adapter;
	}

	public CachingConnectionFactory connectionFactory2(ActiveMQConnectionFactory connectionFactory) {
		CachingConnectionFactory factory = new CachingConnectionFactory(connectionFactory);
		return factory;
	}

	@Bean
	public MessageListenerContainer messageListenerContainer2(@Qualifier("jmsConnectionFactory") ActiveMQConnectionFactory connectionFactory) {
		DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
		container.setConnectionFactory(connectionFactory2(connectionFactory));
		container.setDestination(stateInfoQueue());
		container.setMessageListener(messageListenerAdapter2());
		container.setConcurrency("10-50");
		return container;
	}


	@Bean
	public StateConsumerListener textConsumerListener2() {
		return new StateConsumerListener();
	}
}